package com.mywang.weitravel.modules.mine;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.base.MyApplication;
import com.mywang.weitravel.model.ChangeAvatarResponse;
import com.mywang.weitravel.model.UploadAvatarResponse;
import com.mywang.weitravel.model.UserBean;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;
import com.mywang.weitravel.util.DataCleanManager;
import com.mywang.weitravel.util.FileUtils;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class MinePresenter extends BasePresenter<MineContract.View> implements MineContract.Presenter {

    public MinePresenter(MineContract.View view) {
        super(view);
    }

    @Override
    public void uploadAvatar(String filePath) {
        File photoFile = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), photoFile);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", photoFile.getName(), requestBody);
        RetrofitHelper.getInstance().getServer()
                .uploadAvatar(part)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<UploadAvatarResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mView.showLoading();
                    }

                    @Override
                    protected void onSuccess(UploadAvatarResponse uploadAvatarResponse) {
                        changeAvatar(uploadAvatarResponse.src);
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.hideLoading();
                    }
                });


    }

    private void changeAvatar(String url) {
        RetrofitHelper.getInstance().getServer()
                .changeAvatar(url)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<ChangeAvatarResponse>() {

                    @Override
                    protected void onSuccess(ChangeAvatarResponse changeAvatarResponse) {
                        mView.uploadAvatarSuccessful(changeAvatarResponse.avatar);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                });
    }


    @Override
    public void sign() {
        RetrofitHelper.getInstance().getServer()
                .sign()
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mView.showLoading();
                    }

                    @Override
                    protected void onSuccess(Object object) {
                        mView.signSuccessful();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void calculateCacheSize() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                File commonCacheFile = new File(FileUtils.getDiskCacheDir(MyApplication.getApplication()));
                File glideCacheFile = new File(MyApplication.getApplication().getCacheDir().getPath() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR);
                String cacheSize = DataCleanManager.getFormatSize((DataCleanManager.getFolderSize(commonCacheFile) + DataCleanManager.getFolderSize(glideCacheFile)));
                emitter.onNext(cacheSize);
            }
        }).compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        mView.calculateCacheSizeSuccessful(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void cleanCache() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                File commonCacheFile = new File(FileUtils.getDiskCacheDir(MyApplication.getApplication()));
                File glideCacheFile = new File(MyApplication.getApplication().getCacheDir().getPath() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR);
                DataCleanManager.cleanCustomCache(commonCacheFile.getPath());
                DataCleanManager.cleanCustomCache(glideCacheFile.getPath());
                emitter.onNext("");
            }
        }).compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        mView.cleanCacheSuccessful();
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("清除缓存出现错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getUserInfo() {
        RetrofitHelper.getInstance().getServer()
                .getUserInfo()
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<UserBean>() {

                    @Override
                    protected void onSuccess(UserBean userBean) {
                        mView.getUserInfoSuccessful(userBean);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
